package in.hitmedia.test.arrays;

import java.util.Arrays;

public class RotateMatrix 
{

	public static void rotate(int [][] m)
	{
		int n = m.length;
		System.out.println("n = "+n);
		
		for (int layer = 0; layer < (n/2) ; ++layer)
		{
			int first = layer;
			int last = n - 1 - layer;
			
			for (int i = first; i < last; ++i)
			{
				int offset = i - first;
				
				int top = m[first][i];
				
				m[first][i] = m[last-offset][first];
				
				m[last-offset][first] = m[last][last-offset];
				
				m[last][last-offset] = m[i][last];
				
				m[i][last] = top;
			}
			
			
		}
	}
	
	public static void main(String args[])
	{
		int n = 4;
		int a[][] = new int [n][];
		for (int i=0; i< n; ++i)
		{
			a[i] = new int[n];
			for (int j=0; j < n; ++j )
			{
				a[i][j] = (i)*n + (j+1);
			}
		}
		
		System.out.println(Arrays.deepToString(a));
		rotate(a);
		System.out.println(Arrays.deepToString(a));
		
		
	}
	
}
